package com.bjtech.dao.ibatis;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.ssi.dao.impl.BaseDaoSqlMapImpl;

import com.bjtech.bo.ShareBO;
import com.bjtech.dao.ShareAreaResultDao;
import com.bjtech.domain.BaseBuildingInfo;
import com.bjtech.domain.BaseRoomInfo;
import com.bjtech.domain.BaseShareAreaInfo;
import com.bjtech.domain.BaseUnitInfo;
import com.bjtech.domain.ShareAreaResult;
import com.bjtech.domain.ShareSinkResult;
import com.bjtech.domain.ShareUserResult;
import com.bjtech.web.util.DateUtil;

/**
 * @author wf
 *
 */
@Repository(value="shareAreaResultDao")
public class SqlMapShareAreaResultDao extends BaseDaoSqlMapImpl<ShareAreaResult> implements
		ShareAreaResultDao {

	/**
	 * 分摊操作
	 * @param shareResultId
	 */
//	public String updateSharingResult(Long shareResultId) {
//		String areaResult ="";
//		String userResult ="";
//		areaResult = areaSharing(shareResultId);
//		if(areaResult!=""){
//			return areaResult;
//		}else{
//			userResult = userSharing(shareResultId);
//			if(userResult!=""){
//				return userResult;
//			}
//		}
//		return "";
//	}
	
	/**
	 * 分摊与分摊
	 * @param shareResultId
	 * @return
	 */
//	private String areaSharing(Long shareResultId) {
//		ShareAreaResult areaResult = (ShareAreaResult) super.getSqlMapClientTemplate().queryForObject("selectShareAreaResult",shareResultId);
//		String errMsg="";
//		//计量收费基本热价	BASE_PRICE：按面积收费热价*二步制比例(单位：元/㎡)
//		//计量收费计量热价	CAL_PRICE 及单位CAL_UNIT（"计量收费计量热价"统一转换成MWh）
//		//按面积收费热价	PRICE_AREA(单位：元/㎡)
//		BaseShareAreaInfo shareArea = (BaseShareAreaInfo)super.getSqlMapClientTemplate().queryForObject("selectBaseShareAreaInfo", areaResult.getArea_id());
//		areaResult.setBase_price(shareArea.getBase_price().doubleValue());
//		areaResult.changeCal_price(shareArea.getCal_price().doubleValue(),shareArea.getCal_unit());//（"计量收费计量热价"统一转换成MWh）
//		areaResult.setCal_unit(new Long(2));
//		areaResult.setPrice_area(shareArea.getArea_price().doubleValue());
//		
//		//总热用户数	SUM_USER
//		//总供热面积	SUM_AREA
//		//不参与计量收费的总热用户数	SUM_OTHER_USER
//		//不参与计量收费的总供热面积	SUM_OTHER_AREA
//		HashMap result1 = (HashMap) super.getSqlMapClientTemplate().queryForObject("selectShareOpResult1",areaResult.getArea_id());
//		areaResult.setSum_user((Long) result1.get("USER_COUNT"));
//		areaResult.setSum_area((Double) result1.get("SUM_HEAT_AREA"));
//		areaResult.setSum_other_user(((BigDecimal)result1.get("COUNT_OTHER_USER")).longValue());
//		areaResult.setSum_other_area(((Double)result1.get("SUM_OTHER_AREA")));
//		//分摊域总耗热量	SUM_HEAT:热量表的数据差值 
//		Long start_value = (Long)super.getSqlMapClientTemplate().queryForObject("selectSumHeatBegin", shareResultId);
//		Long end_value = (Long)super.getSqlMapClientTemplate().queryForObject("selectSumHeatEnd", shareResultId);
//		
//		if(start_value==null){
//			errMsg="“分摊周期开始日期”对应的热量表数据不存在";
//		}else if(end_value==null){
//			errMsg="“分摊周期结束日期”对应的热量表数据不存在";
//		}else{
//			areaResult.setSum_heat(start_value.doubleValue()-end_value.doubleValue());
//		}
//		
//		//总基本热费	SUM_BASE_MONEY:计量收费基本热价*总供热面积(单位：元/㎡)
//		areaResult.setSum_base_money(areaResult.getBase_price()*areaResult.getSum_area());
//		
//		//总计量热费	SUM_CAL_MONEY:计量收费计量热价*分摊域总耗热量
//		areaResult.setSum_cal_money(areaResult.getCal_price()*areaResult.getSum_heat());
//		
//		//总热费	SUM_HEAT_MONEY:总基本热费+总计量热费
//		areaResult.setSum_heat_money(areaResult.getSum_base_money()+areaResult.getSum_cal_money());
//		
//		//不参与计量收费部分的总热费	SUM_OTHER_HEAT_MONEY:按面积收费热价*不参与收费的总供热面积(单位：元/㎡)
//		areaResult.setSum_other_heat_money(areaResult.getPrice_area()*areaResult.getSum_other_area());
//		
//		//需要分摊的总热费	SUM_SHARE_MONEY:总热费-（总供热面积-不参与计量收费的总供热面积）*计量收费基本热价-不参与计量收费部分的总热费
//		areaResult.setSum_share_money(areaResult.getSum_heat_money()-(areaResult.getSum_area()-areaResult.getSum_other_area())*areaResult.getBase_price()-areaResult.getSum_other_heat_money());
//		
//		//散热权重合计	SUM_SINK_WEIGHT：所有用户的散热权重合计
//		HashMap map_sink = new HashMap();
//		map_sink.put("area_id", areaResult.getArea_id());
//		map_sink.put("start_date", areaResult.getShare_date());
//		map_sink.put("end_date", areaResult.getEnd_date());
//		HashMap sumSinkWeightResult = (HashMap) super.getSqlMapClientTemplate().queryForObject("selectSumSinkWeight", map_sink);
//		if(sumSinkWeightResult!=null){
//			areaResult.setSum_sink_weight(((Double) sumSinkWeightResult.get("SUM_SINK_WEIGHT")).longValue());
//		}
//		
//		//单位权重热费	PER_WEIGHT_PRICE： 需要分摊的总热费÷散热权重合计
//		areaResult.setPer_weight_price(areaResult.getSum_share_money()/areaResult.getSum_sink_weight());
//		
//		//本次分摊的分配表数量合计	SHARE_METER_NUM
//		HashMap shareMeterNumResult = (HashMap) super.getSqlMapClientTemplate().queryForObject("selectShareMeterNum", areaResult.getArea_id());
//		if(shareMeterNumResult!=null){
//			areaResult.setShare_meter_num((Long) shareMeterNumResult.get("SHARE_METER_NUM"));
//		}
//		//本次分摊的散热器数量合计	SINK_NUM
//		HashMap sinkNumResult = (HashMap) super.getSqlMapClientTemplate().queryForObject("selectSinkNum", areaResult.getArea_id());
//		if(shareMeterNumResult!=null){
//			areaResult.setSink_num((Long) sinkNumResult.get("SINK_NUM"));
//		}
//		
//		//平均热指标	AVG_HEAT_TARGET：分摊域总耗热量×1000000÷总供热面积÷（分摊周期结束日期-分摊周期开始日期） ÷24
//		areaResult.setAvg_heat_target(areaResult.getSum_heat()*1000000/areaResult.getSum_area()/DateUtil.getIntervalDaysOfExitDate(areaResult.getStart_date(),areaResult.getEnd_date())/24);
//		//分摊时间	SHARE_DATE
//		areaResult.setShare_date(new Date());
//		
//		super.getSqlMapClientTemplate().update("updateShareAreaResult",areaResult);
//		return errMsg;
//	}

	/**
	 * 用户分摊
	 * @param shareResultId
	 * @return
	 */
//	private String userSharing(Long shareResultId) {
//		ShareAreaResult areaResult = (ShareAreaResult) super.getSqlMapClientTemplate().queryForObject("selectShareAreaResult",shareResultId);
//		//先删除对应的用户分摊数据和SinkShareResult，再重新插入
//		super.getSqlMapClientTemplate().delete("deleteShareSinkResultByShareResultId", shareResultId);
//		super.getSqlMapClientTemplate().delete("deleteShareUserResultByShareResultId", shareResultId);
//		
//		List<BaseRoomInfo> roomList = super.getSqlMapClientTemplate().queryForList("selectShareRoomList",shareResultId);
//		for(BaseRoomInfo room :roomList){
//			ShareUserResult userResult = new ShareUserResult();
//			userResult.setArear_result_id(shareResultId);
//			userResult.setRoom_id(room.getId());
//			
//			BaseBuildingInfo building = (BaseBuildingInfo) super.getSqlMapClientTemplate().queryForObject("selectBaseBuildingInfo", room.getBuild_id());
//			userResult.setB_name(building.getB_name());
//			
//			BaseUnitInfo unit = (BaseUnitInfo)super.getSqlMapClientTemplate().queryForObject("selectBaseUnitInfo", room.getUnit_no());
//			userResult.setUnit_no(unit.getUnit_name());
//			userResult.setFloor_no(room.getFloor_no());
//			userResult.setHouse_num(room.getHouse_num());
//			userResult.setIs_cal(room.getIs_cal());
//			userResult.setHear_area(room.getHear_area());
//			userResult.setStart_date(areaResult.getStart_date());
//			userResult.setEnd_date(areaResult.getEnd_date());
//			userResult.setXz_factor(room.getXz_factor());
//			
//			Long userResultId = (Long) super.getSqlMapClientTemplate().insert("insertShareUserResult", userResult);
//			userResult.setId(userResultId);
//			
//			HashMap map_param = new HashMap();
//			map_param.put("room_id", room.getId());
//			map_param.put("start_date", areaResult.getStart_date());
//			map_param.put("end_date", areaResult.getEnd_date());
//			List<HashMap> sinkList = super.getSqlMapClientTemplate().queryForList("selectSinkListForShare",map_param);
//			long sink_num=0;
//			for(HashMap sinkMap:sinkList){
//				sink_num++;
//				ShareSinkResult sinkResult = new ShareSinkResult();
//				sinkResult.setUser_result_id(userResultId);
//				sinkResult.setSink_no(((Integer)sinkMap.get("SINK_NO")).longValue());
//				sinkResult.setPower_factor((Float)sinkMap.get("POWER_FACTOR"));
//				sinkResult.setCouple_factor((Float)sinkMap.get("COUPLE_FACTOR"));
//				sinkResult.setOther_factor((Float)sinkMap.get("OTHER_FACTOR"));
//				sinkResult.setMeter_num(((BigDecimal)sinkMap.get("READ_NUM")).longValue());
//				Float weight = sinkResult.getPower_factor()*sinkResult.getCouple_factor()*sinkResult.getOther_factor()*sinkResult.getMeter_num()*room.getXz_factor();
//				sinkResult.setHeat_weight(new Long(weight.longValue()));
//				
//				//分摊比例	SHARE_PERCENT:散热权重÷散热权重合计 
//				sinkResult.setShare_percent(sinkResult.getHeat_weight()/areaResult.getSum_sink_weight().floatValue());
//				//分摊热费	SHARE_HEAT_MONEY:需要分摊的总热费 ×分摊比例
//				sinkResult.setShare_heat_money(areaResult.getSum_share_money().floatValue()*sinkResult.getShare_percent());
//				
//				//参考热消耗量	REFER_HEAT_NUM:分摊热费÷计量收费计量热价 MWH
//				sinkResult.setRefer_heat_num(sinkResult.getShare_heat_money()/areaResult.getCal_price().floatValue());
//				
//				super.getSqlMapClientTemplate().insert("insertShareSinkResult", sinkResult);
//			}
//			
//			//基本热费	BASE_HEAT_MONEY:供热面积×计量收费基本热价
//			userResult.setBase_heat_money(userResult.getHear_area()*areaResult.getBase_price().floatValue());
//			//散热权重	HEAT_WEIGHT:Kq×Kc×Ke×户修正系数×分配表字数
//			//分摊比例	SHARE_PERCENT:散热权重÷散热权重合计 
//			//分摊热费	SHARE_HEAT_MONEY:需要分摊的总热费 ×分摊比例
//			//参考热消耗量	REFER_HEAT_NUM:分摊热费÷计量收费计量热价    从散热器分摊中累计
//			HashMap sumSinkResult = (HashMap) super.getSqlMapClientTemplate().queryForObject("selectSumSinkResult", userResult.getId());
//			userResult.setHeat_weight(((BigDecimal) sumSinkResult.get("HEAT_WEIGHT")).longValue());
//			userResult.setShare_percent(((Double) sumSinkResult.get("SHARE_PERCENT")).floatValue());
//			userResult.setShare_heat_money(((Double) sumSinkResult.get("SHARE_HEAT_MONEY")).floatValue());
//			userResult.setRefer_heat_num(((Double) sumSinkResult.get("REFER_HEAT_NUM")).floatValue());
//			
//			//总热费	TOTAL_HEAT_MONEY:基本热费+分摊热费
//			userResult.setTotal_heat_money(userResult.getBase_heat_money()+userResult.getShare_heat_money());
//			//参考热指标	REFER_HEAT_TARGET:参考热消耗量×1000000÷供热面积÷供热天数÷24
//			Long heatDays = (Long) super.getSqlMapClientTemplate().queryForObject("selectHeatDaysForUserShare", room.getId());
//			userResult.setRefer_heat_target(userResult.getRefer_heat_num()*1000000/userResult.getHear_area()/heatDays/24);
//			//散热器数量合计	SINK_NUM
//			userResult.setSink_num(sink_num);
//
//			super.getSqlMapClientTemplate().update("updateShareUserResult", userResult);
//		}
//		return "";
//	}

	public ShareBO createShareBO(Long shareAreaResultId) {
		ShareBO shareBO = new ShareBO();
		
		ShareAreaResult shareAreaResult = this.selectEntity(new ShareAreaResult(), shareAreaResultId);
		
		//分摊域基础数据
		BaseShareAreaInfo shareAreaInfo = (BaseShareAreaInfo) super.getSqlMapClientTemplate().queryForObject("selectBaseShareAreaInfo",shareAreaResult.getArea_id());
		shareBO.setShareAreaData(shareAreaInfo);
		
		//户基础数据
		HashMap userBaseMap = (HashMap) super.getSqlMapClientTemplate().queryForObject("selectAreaUserBaseData", shareAreaResult.getArea_id());
		shareBO.setUserCount(((Long)userBaseMap.get("userCount")).intValue());
		shareBO.setJoinShareUserCount((((Long) userBaseMap.get("userCount"))).intValue()-(((BigDecimal) userBaseMap.get("notJoinShareUserCount")).intValue()));
		
		
		//散热器基础数据
		Long sinkCount = (Long) super.getSqlMapClientTemplate().queryForObject("selectAreaSinkCount", shareAreaResult.getArea_id());
		shareBO.setSinkCount(sinkCount.intValue());
		
		//分配表安装情况
		Long sinkWithMeterCount = (Long)super.getSqlMapClientTemplate().queryForObject("selectSinkWithMetetCount", shareAreaResult.getArea_id());
		shareBO.setSinkWithMeterCount(sinkWithMeterCount.intValue());
		shareBO.setSinkNoMeterCount(shareBO.getSinkCount().intValue()-sinkWithMeterCount.intValue());
		if(shareBO.getSinkCount()!=null&&shareBO.getSinkCount().intValue()!=0){
			shareBO.setSinkWithMeterPercent(shareBO.getSinkWithMeterCount().floatValue()/shareBO.getSinkCount().floatValue()*100);
		}
		
		//热量表
		Long heatMeterCount = (Long) super.getSqlMapClientTemplate().queryForObject("selectAreaHeatMeterCount", shareAreaResult.getArea_id());
		shareBO.setHeatMeterCount(heatMeterCount.intValue());
		
		HashMap paramMap2= new HashMap();
		paramMap2.put("area_result_id", shareAreaResult.getId());
		paramMap2.put("area_id", shareAreaResult.getArea_id());
		
		Long heatMeterDataCount = (Long) super.getSqlMapClientTemplate().queryForObject("selectAreaHeatMeterDataCount", paramMap2);
		shareBO.setHeatMeterNoDataCount(shareBO.getHeatMeterCount().intValue()-heatMeterDataCount.intValue());
		
		//分配表总数
		Long assignMeterCount = (Long) super.getSqlMapClientTemplate().queryForObject("selectAreaAssignMeterCount", shareAreaResult.getArea_id());
		shareBO.setAssignMeterCount(assignMeterCount.intValue());
		
		//分配表起始抄表数据
		HashMap paramMap3= new HashMap();
		paramMap3.put("area_result_id", shareAreaResult.getId());
		paramMap3.put("area_id", shareAreaResult.getArea_id());
		Long assignMeterStartDateCount = (Long)super.getSqlMapClientTemplate().queryForObject("selectAssignMeterStartDataCount",paramMap3);
		shareBO.setAssignMeterStartDateCount(assignMeterStartDateCount.intValue());
		shareBO.setAssignMeterStartNoDateCount(shareBO.getAssignMeterCount()-shareBO.getAssignMeterStartDateCount());
		shareBO.setAssignStartPercent(shareBO.getAssignMeterStartDateCount()*100/shareBO.getAssignMeterCount());
		
		HashMap paramMap4= new HashMap();
		paramMap4.put("area_result_id", shareAreaResult.getId());
		paramMap4.put("area_id", shareAreaResult.getArea_id());
		Long assignMeterEndDateCount = (Long)super.getSqlMapClientTemplate().queryForObject("selectAssignMeterEndDataCount",paramMap4);
		shareBO.setAssignMeterEndDateCount(assignMeterEndDateCount.intValue());
		shareBO.setAssignMeterEndNoDateCount(shareBO.getAssignMeterCount()-shareBO.getAssignMeterEndDateCount());
		shareBO.setAssignEndPercent(shareBO.getAssignMeterEndDateCount()*100/shareBO.getAssignMeterCount());
		
		if(shareBO.getAssignStartPercent()!=null&&shareBO.getAssignEndPercent()!=null
				&&shareBO.getAssignStartPercent()>=100&&shareBO.getAssignEndPercent()>=100
				&&shareBO.getHeatMeterNoDataCount()!=null&shareBO.getHeatMeterCount()!=null
				&&shareBO.getHeatMeterNoDataCount()==0){
			shareBO.setCal_share(1);
		}else{
			shareBO.setCal_share(0);
		}
		return shareBO;
	}

	public Integer updateSharingResultByProcedure(Map param) {
		super.getSqlMapClientTemplate().queryForObject("updateSharingResultByProcedure",param);
		return (Integer) param.get("share_result");
	}

	public void deleteAllResult(Long shareAreaId) {
		super.getSqlMapClientTemplate().delete("deleteShareAreaResult",shareAreaId);
		super.getSqlMapClientTemplate().delete("deleteShareUserResultByShareResultId",shareAreaId);
		super.getSqlMapClientTemplate().delete("deleteShareSinkResultByShareResultId",shareAreaId);
	}
}
